package com.bbs.util;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @PackageName: org.example.util
 * @ClassName: SensitiveWordUtil
 * @Description: 敏感词工具类
 * @Author: Rambo
 * @Data: 2024/6/23 15:47
 */
@Component
public class SensitiveWordUtil {

    private static SensitiveWordBs sensitiveWordBs;

    @Autowired
    public SensitiveWordUtil(SensitiveWordBs sensitiveWordBs) {
        SensitiveWordUtil.sensitiveWordBs = sensitiveWordBs;
    }

    // 刷新敏感词库与非敏感词库缓存
    public static void refresh() {
        sensitiveWordBs.init();
    }

    // 判断是否含有敏感词
    public static boolean contains(String text) {
        return sensitiveWordBs.contains(text);
    }

    // 指定替换符进行替换敏感词
    public static String replace(String text, char replaceChar) {
        return sensitiveWordBs.replace(text, replaceChar);
    }

    // 使用默认替换符 * 进行替换敏感词
    public static String replace(String text) {
        return sensitiveWordBs.replace(text);
    }

    // 返回所有敏感词
    public static List<String> findAll(String text) {
        return sensitiveWordBs.findAll(text);
    }
}
